package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerRegisterActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.RegisterActivityModule;
import com.kucai.yifaqapp.model.network.entity.res.ResCode;
import com.kucai.yifaqapp.model.network.entity.res.Resregister;
import com.kucai.yifaqapp.presenter.activity.RegisterActivityPresenter;
import com.kucai.yifaqapp.ui.view.RegisterActivityView;
import com.kucai.yifaqapp.util.Md5;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.OnClick;

public class RegisterActivity extends BaseActivity<RegisterActivityPresenter> implements RegisterActivityView {

    TextView titleLeftTv;
    TextView headerTitle;
    EditText ReferrerUserEt;
    EditText usernameEt;
    EditText passwordEt;
    EditText password2Et;
    EditText realnameEt;
    EditText phoneEt;
    EditText codeEt;
    Button registerBt;
    CheckBox yearpass2;
    CheckBox yearpass;
    TextView loginBt;
    ImageView code_img;
    LinearLayout phonelin;
    LinearLayout skinbg;
    View register_view;
    private String peuser;
    private String username;
    private String password;
    private String password2;
    private String realname;
    private String phone;
    private String code;
    private String codes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.title_left_tv);
        headerTitle = findViewById(R.id.headerTitle);
        ReferrerUserEt = findViewById(R.id.Referrer_user_et);
        usernameEt = findViewById(R.id.username_et);
        passwordEt = findViewById(R.id.password_et);
        password2Et = findViewById(R.id.password2_et);
        realnameEt = findViewById(R.id.realname_et);

        yearpass = findViewById(R.id.yearpass);
        yearpass2 = findViewById(R.id.yearpass2);
        phoneEt = findViewById(R.id.phone_et);
        codeEt = findViewById(R.id.code_et);
        registerBt = findViewById(R.id.register_bt);
        loginBt = findViewById(R.id.login_bt);
        code_img = findViewById(R.id.code_img);
        phonelin = findViewById(R.id.phonelin);
        skinbg = findViewById(R.id.skinbg);
        register_view = findViewById(R.id.register_view);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        mPresenter.getCode();
        headerTitle.setText("注册");
        titleLeftTv.setText("返回");
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ReferrerUserEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    register_view.setVisibility(View.VISIBLE);
                } else {
                    register_view.setVisibility(View.GONE);
                }
            }
        });
        usernameEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    register_view.setVisibility(View.VISIBLE);
                } else {
                    register_view.setVisibility(View.GONE);
                }
            }
        });
        passwordEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    register_view.setVisibility(View.VISIBLE);
                } else {
                    register_view.setVisibility(View.GONE);
                }
            }
        });
        password2Et.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    register_view.setVisibility(View.VISIBLE);
                } else {
                    register_view.setVisibility(View.GONE);
                }
            }
        });
        realnameEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    register_view.setVisibility(View.VISIBLE);
                } else {
                    register_view.setVisibility(View.GONE);
                }
            }
        });
        phoneEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    register_view.setVisibility(View.VISIBLE);
                } else {
                    register_view.setVisibility(View.GONE);
                }
            }
        });
        codeEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    register_view.setVisibility(View.VISIBLE);
                } else {
                    register_view.setVisibility(View.GONE);
                }
            }
        });

        yearpass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = passwordEt.getText().length();
                passwordEt.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                passwordEt.setSelection(passwordLength);
            }
        });
        yearpass2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = password2Et.getText().length();
                password2Et.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                password2Et.setSelection(passwordLength);
            }
        });
    }

    @Override
    protected void initDagger() {
        DaggerRegisterActivityComponent.builder()
                .registerActivityModule(new RegisterActivityModule(this))
                .build().inject(this);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_register_activty;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @OnClick({R.id.register_bt, R.id.login_bt, R.id.code_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_bt:
                peuser = ReferrerUserEt.getText().toString().trim();//推荐人
                username = usernameEt.getText().toString().trim();//账号
                password = passwordEt.getText().toString().trim();//密码
                password2 = password2Et.getText().toString().trim();//新密码
                realname = realnameEt.getText().toString().trim();//真实姓名
                phone = phoneEt.getText().toString().trim();//手机号
                code = codeEt.getText().toString().trim();//用户输入的验证码
//                if (TextUtils.isEmpty(peuser)) {
//                    Toast.makeText(RegisterActivity.this, "请输入推荐人", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(RegisterActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password2)) {
                    Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(realname)) {
                    Toast.makeText(RegisterActivity.this, "请输入真实姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
//                if (TextUtils.isEmpty(phone)) {
//                    Toast.makeText(RegisterActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(RegisterActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
//                if (Phoneutil.isMobileNO(phone) == false) {
//                    Toast.makeText(RegisterActivity.this, "手机号格式错误", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (!password.equals(password2)) {
                    Toast.makeText(RegisterActivity.this, "两次密码必须一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                //                if (!isMatcherFinded("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$", password)) {
//                    Toast.makeText(RegisterActivity.this, "密码必须为6-16位字母和数字组成", Toast.LENGTH_LONG).show();
//                    return;
//                }


                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.isEmpty(password2)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!isMatcherFinded("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$", username)) {
                    Toast.makeText(RegisterActivity.this, "账号必须为6-15位字母和数字组成", Toast.LENGTH_LONG).show();
                    return;
                }
                String password1 = Md5.toMd5(password);
                mPresenter.register(username, password1, realname, phone, peuser, codes, code);
                break;
            case R.id.login_bt:
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
                break;
            case R.id.code_img:
                mPresenter.getCode();
                break;
        }
    }

    @Override
    public void onCode(ResCode resCode) {
        switch (resCode.data.register_type) {
            case 0:
                phonelin.setVisibility(View.GONE);
                break;
            case 1:
                phonelin.setVisibility(View.VISIBLE);
                phoneEt.setHint("11位手机号");
                break;
            case 2:
                phonelin.setVisibility(View.VISIBLE);
                phoneEt.setHint("请输入qq号");
                break;
            case 3:
                phonelin.setVisibility(View.VISIBLE);
                phoneEt.setHint("请输入微信号");
                break;
            default:
                Toast.makeText(RegisterActivity.this, "验证码接口返回有误", Toast.LENGTH_SHORT).show();
                break;
        }
        codes = "";
        codes = resCode.data.captcha_code;
        Bitmap bitmap = null;
        byte[] bitmapArray;
        bitmapArray = Base64.decode(resCode.data.code, Base64.NO_WRAP);
        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        code_img.setImageBitmap(bitmap);//显示图片

    }

    @Override
    public void onRegister() {
        mPresenter.getCode();
    }

    @Override
    public void onUsermessage(Resregister resregister) {
        //轮询用户信息
        mPresenter.finduser(resregister.data.token);
    }

    public static boolean isMatcherFinded(String patternStr, CharSequence input) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            return true;
        }
        return false;
    }

}
